<template>
  <div class="vintage-moments">
    <el-empty v-if="moments.length === 0" description="还没有记录任何心情" />

    <div v-else class="moment-cards">
      <div
        v-for="moment in moments"
        :key="moment.id"
        class="moment-card"
      >
        <!-- 纸张纹理背景 -->
        <div class="paper-texture"></div>

        <!-- 装饰性边框 -->
        <div class="decorative-border"></div>

        <div class="moment-header">
          <div class="moment-meta">
            <span class="moment-date">{{ formatDate(moment.createdAt) }}</span>
            <el-tag
              v-if="moment.mood"
              :type="getMoodTagType(moment.mood)"
              size="small"
            >
              {{ getMoodLabel(moment.mood) }}
            </el-tag>
            <el-tag v-if="moment.isPrivate" type="info" size="small">
              <el-icon><Lock /></el-icon>
              私密
            </el-tag>
          </div>
          <div class="moment-actions">
            <el-button
              size="small"
              text
              @click="$emit('edit', moment.id)"
            >
              <el-icon><Edit /></el-icon>
            </el-button>
            <el-button
              size="small"
              text
              type="danger"
              @click="$emit('delete', moment.id)"
            >
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
        </div>

        <div class="moment-content">
          <p class="moment-text">{{ moment.content }}</p>

          <div v-if="moment.images && moment.images.length > 0" class="moment-images">
            <el-image
              v-for="(image, index) in moment.images"
              :key="index"
              :src="image"
              fit="cover"
              class="moment-image"
              :preview-src-list="moment.images"
              :initial-index="index"
            />
          </div>

          <div v-if="moment.tags && moment.tags.length > 0" class="moment-tags">
            <el-tag
              v-for="tag in moment.tags"
              :key="tag"
              size="small"
              effect="plain"
              @click="$emit('search-tag', tag)"
              class="tag-clickable"
            >
              #{{ tag }}
            </el-tag>
          </div>
        </div>

        <!-- 装饰性花朵 -->
        <div class="decoration-flower">✿</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Edit, Delete, Lock } from '@element-plus/icons-vue'
import type { Moment, MoodType } from '../types'

interface Props {
  moments: Moment[]
}

defineProps<Props>()

defineEmits<{
  edit: [id: string]
  delete: [id: string]
  'search-tag': [tag: string]
}>()

// 心情选项
const moodOptions = [
  { value: 'happy', label: '😊 开心' },
  { value: 'sad', label: '😢 难过' },
  { value: 'excited', label: '🎉 兴奋' },
  { value: 'peaceful', label: '😌 平静' },
  { value: 'anxious', label: '😰 焦虑' },
  { value: 'grateful', label: '🙏 感恩' },
  { value: 'thoughtful', label: '🤔 沉思' },
  { value: 'nostalgic', label: '😌 怀念' }
]

// 获取心情标签类型
const getMoodTagType = (mood: MoodType) => {
  const typeMap = {
    happy: 'success',
    excited: 'success',
    grateful: 'success',
    peaceful: 'info',
    thoughtful: 'info',
    nostalgic: 'info',
    sad: 'warning',
    anxious: 'danger'
  }
  return typeMap[mood as keyof typeof typeMap] || 'info'
}

// 获取心情标签文本
const getMoodLabel = (mood: MoodType) => {
  const option = moodOptions.find(opt => opt.value === mood)
  return option?.label || mood
}

// 格式化日期
const formatDate = (dateStr: string) => {
  const date = new Date(dateStr)
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))

  if (days === 0) {
    return '今天 ' + date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    })
  } else if (days === 1) {
    return '昨天 ' + date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    })
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return date.toLocaleDateString('zh-CN')
  }
}
</script>

<style scoped>
.vintage-moments {
  height: 100%;
  overflow-y: auto;
  padding: 0 10px;
}

.moment-cards {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding-bottom: 20px;
}

/* 单个卡片 - 手账页面风格 */
.moment-card {
  background: #fefdfb;
  padding: 40px;
  position: relative;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border: 1px solid #d4c5b0;
  animation: fadeInUp 0.6s ease-out;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.moment-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 纸张纹理 */
.paper-texture {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    repeating-linear-gradient(
      90deg,
      transparent,
      transparent 2px,
      rgba(139, 115, 85, 0.02) 2px,
      rgba(139, 115, 85, 0.02) 3px
    ),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(139, 115, 85, 0.02) 2px,
      rgba(139, 115, 85, 0.02) 3px
    );
  pointer-events: none;
  opacity: 0.5;
}

/* 装饰性边框 */
.decorative-border {
  position: absolute;
  top: 15px;
  left: 15px;
  right: 15px;
  bottom: 15px;
  border: 2px solid #c9a876;
  pointer-events: none;
}

.moment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  position: relative;
  z-index: 1;
}

.moment-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* 日期邮戳风格 */
.moment-date {
  background: rgba(218, 165, 32, 0.15);
  border: 2px solid #b8860b;
  border-radius: 50%;
  padding: 8px 12px;
  font-size: 12px;
  font-family: 'Courier New', monospace;
  color: #8b6914;
  font-weight: bold;
  box-shadow: inset 0 0 10px rgba(184, 134, 11, 0.2);
  transform: rotate(-5deg);
  min-width: 60px;
  text-align: center;
}

.moment-actions {
  display: flex;
  gap: 4px;
  opacity: 0.7;
  transition: opacity 0.3s ease;
}

.moment-card:hover .moment-actions {
  opacity: 1;
}

.moment-content {
  line-height: 1.8;
  position: relative;
  z-index: 1;
}

/* 手写风格的文本 */
.moment-text {
  margin: 0 0 20px 0;
  font-size: 16px;
  line-height: 2;
  color: #4a4a4a;
  font-family: 'STKaiti', 'KaiTi', serif;
  white-space: pre-wrap;
  text-align: justify;
}

.moment-images {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.moment-image {
  width: 100px;
  height: 100px;
  border-radius: 4px;
  cursor: pointer;
  border: 3px solid #e8d5b7;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s ease;
}

.moment-image:hover {
  transform: scale(1.05) rotate(2deg);
}

/* 复古标签风格 */
.moment-tags {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.moment-tags :deep(.el-tag) {
  background: #e8d5b7;
  color: #5d4037;
  border: 2px dashed #a67c52;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  font-family: 'Georgia', serif;
  padding: 6px 14px;
}

.tag-clickable {
  cursor: pointer;
  transition: all 0.2s ease;
}

.tag-clickable:hover {
  transform: rotate(-3deg) scale(1.05);
  background: #d4c5a0 !important;
}

/* 心情标签贴纸风格 */
.moment-meta :deep(.el-tag) {
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
  transform: rotate(-3deg);
  border: none;
}

.moment-meta :deep(.el-tag.el-tag--success) {
  background: #f4a460;
  color: white;
}

.moment-meta :deep(.el-tag.el-tag--info) {
  background: #a9a9a9;
  color: white;
}

.moment-meta :deep(.el-tag.el-tag--warning) {
  background: #6495ed;
  color: white;
}

.moment-meta :deep(.el-tag.el-tag--danger) {
  background: #cd5c5c;
  color: white;
}

/* 私密标签 */
.moment-meta :deep(.el-tag:has(.el-icon)) {
  background: #9370db;
  color: white;
  transform: rotate(5deg);
}

/* 装饰性花朵 */
.decoration-flower {
  position: absolute;
  bottom: 30px;
  left: 40px;
  font-size: 32px;
  color: #d4af37;
  opacity: 0.4;
  transform: rotate(-20deg);
  z-index: 0;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .moment-card {
    padding: 25px 20px;
  }

  .decorative-border {
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
  }

  .moment-date {
    transform: rotate(0);
    font-size: 11px;
    padding: 6px 10px;
  }

  .moment-images {
    gap: 8px;
  }

  .moment-image {
    width: 70px;
    height: 70px;
  }
}
</style>
